﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security;
using System.Xml;
using System.IO;
using System.Net;

namespace MindFuck
{
    public partial class Form1 : Form
    {
        Brush brushRect;
        Graphics graphicRect;
        Rectangle editRect;
        
        PictureBox pb;
        Image image;

        ToolTip uploadTip = new ToolTip();
        ToolTip deleteTip = new ToolTip();
        ToolTip exitTip = new ToolTip();
        ToolTip editTip = new ToolTip();
        ToolTip sendTip = new ToolTip();

        bool editOn = false;

        public Form1()
        {
            InitializeComponent();
            brushRect = new SolidBrush(Color.LightSkyBlue);
            editRect = new Rectangle(200, 200, 50, 50);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void Upload_Click(object sender, EventArgs e)
        {
            Upload.Hide();
            //define properties of control
            ofd1.Multiselect = false;
            ofd1.Title = "Localizar Arquivo";
            ofd1.InitialDirectory = @"C:\";
            //Use filer to show only images files
            ofd1.Filter = "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" + "All files (*.*)|*.*";
            ofd1.CheckFileExists = true;
            ofd1.CheckPathExists = true;
            ofd1.FilterIndex = 1;
            ofd1.RestoreDirectory = false;
            ofd1.ReadOnlyChecked = true;
            ofd1.ShowReadOnly = true;
            
            DialogResult dr = ofd1.ShowDialog();

            if (dr == System.Windows.Forms.DialogResult.Cancel)
            {
                Upload.Show();
            }
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                // Read selectable file 
                foreach (String file in ofd1.FileNames)
                {
                    // PictureBox create
                    try
                    {
                        pb = new PictureBox();
                        image = Image.FromFile(file);
                        pb.SizeMode = PictureBoxSizeMode.StretchImage;

                        //attaches the image to the picture box - pb
                        pb.Image = image;
                        pb.Height = flowLayoutPanel1.Height;
                        pb.Width = flowLayoutPanel1.Width;

                        //includes the image in the FlowLayoutPanel
                        flowLayoutPanel1.Controls.Add(pb);

                    }
                    catch (SecurityException ex)
                    {
                        // The user does not have permission to read files
                        MessageBox.Show("Erro de segurança Contate o administrador de segurança da rede.\n\n" +
                                                    "Mensagem : " + ex.Message + "\n\n" +
                                                    "Detalhes (enviar ao suporte):\n\n" + ex.StackTrace);
                        Upload.Show();
                    }
                    catch (Exception ex)
                    {
                        //Can not load the image (permission issues)
                        MessageBox.Show("Não é possível exibir a imagem : " + file.Substring(file.LastIndexOf('\\'))
                                                   + ". Você pode não ter permissão para ler o arquivo , ou " +
                                                   " ele pode estar corrompido.\n\nErro reportado : " + ex.Message);
                        Upload.Show();
                    }
                }
            }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            // Delete the imagem and "draw" the button upload again.
            flowLayoutPanel1.Controls.Remove(pb);
            Upload.Show();
            Edit.Show();
            editOn = false;
            flowLayoutPanel1.Refresh();
        }

        private void Send_Click(object sender, EventArgs e)
        {
            // Adiciona os textos de comentarios no richText e verifica se os campos name e comment foram preenchidos.
            if (richTextBox1.TextLength > 0 && userName.TextLength > 0 && flowLayoutPanel1 != null)
            {
                DialogResult sendOk = MessageBox.Show("Successful", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (sendOk == DialogResult.OK)
                {
                    userName.Text = null;
                    richTextBox1.Clear();
                    //delete_Click(sender, e);
                    flowLayoutPanel1 = null;
                }
            }
            else
            {
                MessageBox.Show("The 'comment' or 'Name' or 'Upload' field/button is empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            #region Server communication

            // Get the object used to communicate with the server.
            Uri myUri = new Uri("ftp://201.17.38.104:60");
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(myUri);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential("dkg_dan", "dkg_brastemp");


            // This example assumes the FTP site uses anonymous logon.
            /*using (var resp = (FtpWebResponse)request.GetResponse())
            {
                Console.WriteLine(resp.StatusCode);
            }*/

            #endregion

            #region "xml" creator
            // Cria o "xml".
            XmlTextWriter writer = new XmlTextWriter("MindFuck.xml", Encoding.UTF8);

            // Inicio.
            writer.WriteStartDocument();
            writer.WriteStartElement("SEND");

            // Aqui escreve o nome de usuário.
            writer.WriteStartElement("USERNAME");
            writer.WriteString(userName.Text);
            writer.WriteEndElement();
            
            // Aqui é o que escreve no comentário.
            writer.WriteStartElement("COMMENT");
            writer.WriteString(richTextBox1.Text);
            writer.WriteEndElement();

            // Fim.
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();

            #endregion

            Console.WriteLine("enviado");
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            // Fecha o programa.
            DialogResult result = MessageBox.Show("Close application?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (result == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void userName_TextChanged(object sender, EventArgs e)
        {

        }

        public void Edit_Click(object sender, EventArgs e)
        {
            editOn = true;
            if (editOn)
            {
                Edit.Hide();
                graphicRect = flowLayoutPanel1.CreateGraphics();
                graphicRect.FillRectangle(brushRect, editRect);
            }
        }

        private void flowLayoutPanel1_Paint_1(object sender, PaintEventArgs e)
        {
            
        }

        #region Buttons show and hide, toolTip too

        /// <summary>
        /// Quando mouse passa pelo botao ele "aparece"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Upload_MouseEnter(object sender, EventArgs e)
        {
            Upload.Location = new Point(0, 41);

            // Set up the delays for the ToolTip.
            uploadTip.AutoPopDelay = 5000;
            uploadTip.InitialDelay = 500;
            uploadTip.ReshowDelay = 500;
            // Force the ToolTip text to be displayed whether or not the form is active.
            uploadTip.ShowAlways = false;

            // Set up the ToolTip text for the Button and Checkbox.
            uploadTip.SetToolTip(Upload, "Upload");
        }

        /// <summary>
        /// Quando o mouse sai de cima do botao ele se "esconde"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Upload_MouseLeave(object sender, EventArgs e)
        {
            Upload.Location = new Point(-45, 41);
            uploadTip.RemoveAll();
        }

        private void delete_MouseEnter(object sender, EventArgs e)
        {
            delete.Location = new Point(0, 154);

            // Set up the delays for the ToolTip.
            deleteTip.AutoPopDelay = 5000;
            deleteTip.InitialDelay = 500;
            deleteTip.ReshowDelay = 500;
            // Force the ToolTip text to be displayed whether or not the form is active.
            deleteTip.ShowAlways = true;

            // Set up the ToolTip text for the Button and Checkbox.
            deleteTip.SetToolTip(delete, "Delete");
        }

        private void delete_MouseLeave(object sender, EventArgs e)
        {
            delete.Location = new Point(-45, 154);
            deleteTip.RemoveAll();
        }

        private void Exit_MouseEnter(object sender, EventArgs e)
        {
            Exit.Location = new Point(0, 209);

            // Set up the delays for the ToolTip.
            exitTip.AutoPopDelay = 5000;
            exitTip.InitialDelay = 500;
            exitTip.ReshowDelay = 500;
            // Force the ToolTip text to be displayed whether or not the form is active.
            exitTip.ShowAlways = true;

            // Set up the ToolTip text for the Button and Checkbox.
            exitTip.SetToolTip(Exit, "Exit");
        }

        private void Exit_MouseLeave(object sender, EventArgs e)
        {
            Exit.Location = new Point(-45, 209);
            exitTip.RemoveAll();
        }

        private void Edit_MouseEnter_1(object sender, EventArgs e)
        {
            Edit.Location = new Point(0, 97);

            // Set up the delays for the ToolTip.
            editTip.AutoPopDelay = 5000;
            editTip.InitialDelay = 500;
            editTip.ReshowDelay = 500;
            // Force the ToolTip text to be displayed whether or not the form is active.
            editTip.ShowAlways = true;

            // Set up the ToolTip text for the Button and Checkbox.
            editTip.SetToolTip(Edit, "Edit");
        }

        private void Edit_MouseLeave_1(object sender, EventArgs e)
        {
            Edit.Location = new Point(-45, 97);
            editTip.RemoveAll();
        }

        private void Send_MouseEnter(object sender, EventArgs e)
        {
            // Set up the delays for the ToolTip.
            sendTip.AutoPopDelay = 5000;
            sendTip.InitialDelay = 500;
            sendTip.ReshowDelay = 500;
            // Force the ToolTip text to be displayed whether or not the form is active.
            sendTip.ShowAlways = true;

            // Set up the ToolTip text for the Button and Checkbox.
            sendTip.SetToolTip(Send, "Send");
        }

        private void Send_MouseLeave(object sender, EventArgs e)
        {
            sendTip.RemoveAll();
        }

        #endregion

    }
}